<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Flashgame
 * @version    $Id: Create.php 6038 2010 YouNet $
 * @author     To Anh
 */
                     
class Flashgame_Form_Create extends Engine_Form
{
  public $_error = array();

  public function init()
  {
    $this->setTitle('Post New Game')
      ->setDescription('Compose your new game below, then click "Post Game" to publish the game.')
      ->setAttrib('name', 'flashgames_create');

    $this->addElement('Text', 'title', array(
      'label' => 'Game Title',
      'autocomplete' => 'off',  
      'description' => 'Special characters are not allowed.',
      'filters' => array(
       'StripTags', 
        new Engine_Filter_Censor(),
      ),
    ));
    $this->title->getDecorator("Description")->setOption("placement", "append");
    $this->addElement('Textarea', 'body', array(
      'label' => 'Description',
      'filters' => array(
        //'StripTags',
        new Engine_Filter_EnableLinks(),
        new Engine_Filter_Censor(),
      ),
      
    ));
    $this->body->getDecorator("Description")->setOption("placement", "append");    
    $user = Engine_Api::_()->user()->getViewer();
    $user_level = Engine_Api::_()->user()->getViewer()->level_id;
// Init Game
    $game_options = Array();
    $game_options[''] =  "Choose";
    $game_options['1'] =  "My Computer";
    $game_options['2'] = "Embedded Script";
    $this->addElement('Select', 'type', array(
      'label' => 'Game Source',
      'required' => false,
      'multiOptions' => $game_options,
      'onchange' => "updateTextFields(this.value)"
    ));

    $this->addElement('File', 'file_id',   array(
         'label' => 'Game file',
         'autocomplete' => 'off',  
         'description' => 'Only allow .swf file type'
        ));
    $this->file_id->getDecorator("Description")->setOption("placement", "append");
    $this->addElement('Textarea', 'embed_script', array(
      'label' => 'Embedded Script',
    ));
    
   $this->file_id->addValidator('Extension', false, 'swf');

    
    $this->addElement(
        'Text', 
        'width',
        array
        (
             'label'=>'Flash\'s width (px)',
             'autocomplete' => 'off',  
             'description' => 'Default is 0.',  
             'filters' => array(
                new Zend_Filter_Int(),
            ),
        )
    );
    $this->width->getDecorator("Description")->setOption("placement", "append"); 
    $this->addElement(
        'Text', 
        'height', 
        
        array
        (
            'label'=>'Flash\'s height (px)',
            'autocomplete' => 'off',
            'description' => 'Default is 0.',
            'filters' => array(
                new Zend_Filter_Int(),
            ),
        )
    );
    $this->height->getDecorator("Description")->setOption("placement", "append"); 
    
    $this->addElement('Text', 'tags',array(
      'label'=>'Tags (Keywords)',
      'autocomplete' => 'off',
      'description' => 'Separate tags with commas.',
      'filters' => array(
        new Engine_Filter_Censor(),
      ),
    ));
    $this->tags->getDecorator("Description")->setOption("placement", "append");    
    // prepare categories
    $categories = Engine_Api::_()->flashgame()->getCategories();
    if (count($categories)!=0){
      $categories_prepared[0]= "";
      foreach ($categories as $category){
        $categories_prepared[$category->category_id]= $category->category_name;
      }

      // category field
      $this->addElement('Select', 'category_id', array(
            'label' => 'Category',
            'multiOptions' => $categories_prepared
          ));
    }
    
    $this->addElement('File', 'photo_id', array(
        'label' => 'Game\'s Avatar',
        'description' => 'Only allow .jpg, .gif and .png file type',
    ));
    $this->photo_id->addValidator('Extension', false, 'jpg,png,gif');
   $this->photo_id->getDecorator("Description")->setOption("placement", "append"); 
    // View
    $availableLabels = array(
      'everyone'       => 'Everyone',
      'owner_network' => 'Friends and Networks',
      'owner_member_member'  => 'Friends of Friends',
      'owner_member'         => 'Friends Only',
      'owner'          => 'Just Me'
    );

    $options = (array) Engine_Api::_()->authorization()->getAdapter('levels')->getAllowed('flashgame', $user, 'auth_view');
    $options = array_intersect_key($availableLabels, array_flip($options));

    $this->addElement('Select', 'auth_view', array(
      'label' => 'Privacy',
      'description' => 'Who may see this game?',
      'multiOptions' => $options,
      'value' => 'everyone',
    ));
    $this->auth_view->getDecorator('Description')->setOption('placement', 'append');

    $options =(array) Engine_Api::_()->authorization()->getAdapter('levels')->getAllowed('flashgame', $user, 'auth_comment');
    $options = array_intersect_key($availableLabels, array_flip($options));

    // Comment
    $this->addElement('Select', 'auth_comment', array(
      'label' => 'Comment Privacy',
      'description' => 'Who may post comments on this game?',
      'multiOptions' => $options,
      'value' => 'everyone',
    ));
    $this->auth_comment->getDecorator('Description')->setOption('placement', 'append');


    $this->addElement('Button', 'submit', array(
      'label' => 'Post Game',
      'type' => 'submit',
      'decorators' => array(array('ViewScript', array(
        'viewScript' => '_formButtonCancel.tpl',
        'class'      => 'form element'
    )))));

  }

}